package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.base.ConditionDTO;
import com.hang.model.dto.request.*;
import com.hang.model.vo.BugInfoDetailVO;
import com.hang.model.vo.BugInfoVOList;
import com.hang.service.IBugService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "缺陷管理模块")
@RestController
@RequestMapping("bug")
@Slf4j
public class BugController {
    @Resource
    private IBugService bugService;

    @ApiOperation(value = "新增缺陷")
    @PostMapping("create")
    public Result<Object> addBug(
            @RequestBody @Valid BugModelReq bugModelReq
    ){
        try {
            bugService.createBug(bugModelReq);
            return Result.ok().message("新增成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "缺陷大厅查看未被接单的缺陷")
    @PostMapping("hall")
    public Result<BugInfoVOList> getIncompleteBugInHall(
            @RequestBody ItemConditionDTO conditionDTO
    ){
        BugInfoVOList bugInfoVOList = bugService.getIncompleteBugInHall(conditionDTO);
        return Result.ok(bugInfoVOList);
    }

    @ApiOperation(value = "按条件搜索缺陷")
    @PostMapping("getBugListByCondition")
    public Result<BugInfoVOList> getBugListByCondition(
            @RequestBody @Valid BugConditionDTO bugConditionDTO
    ){
        BugInfoVOList bugInfoVOList = bugService.getBugListByCondition(bugConditionDTO);
        return Result.ok(bugInfoVOList);
    }

    @ApiOperation(value = "更新缺陷信息")
    @PostMapping("update")
    public Result<Object> updateBugInfo(
            @RequestBody @Valid BugModelReq bugModelReq
    ){
        try {
            bugService.updateBug(bugModelReq);
            return Result.ok().message("更新成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "批量删除缺陷")
    @DeleteMapping("deleteBatch")
    public Result<Object> deleteBug(
            @RequestBody @Valid BugDeleteDTO bugDeleteDTO
    ){
        try {
            bugService.deleteBugList(bugDeleteDTO);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "查看缺陷详情")
    @GetMapping("detail")
    public Result<BugInfoDetailVO> getBugDetail(
            @RequestParam("bugId") Long bugId
    ){
        BugInfoDetailVO bugInfoDetailVO = bugService.getDetail(bugId);
        return Result.ok(bugInfoDetailVO);
    }

    @ApiOperation(value = "修改缺陷处理状态[处理缺陷]")
    @PostMapping("updateStatus")
    public Result<Object> updateBugStatus(
            @RequestBody @Valid BugUpdateReq bugUpdateReq
    ){
        try {
            bugService.updateBugStatus(bugUpdateReq);
            return Result.ok().message("已修改缺陷状态");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "缺陷接单")
    @PostMapping("receive")
    public Result<Object> receiveOrders(
            @RequestBody @Valid BugReceiveDTO bugReceiveDTO
    ){
        try {
            bugService.receivingBug(bugReceiveDTO);
            return Result.ok().message("已将该缺陷添加到你的待处理缺陷中");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }
}